package com.boruishangge.persagysdk.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.boruishangge.persagysdk.R;
import com.boruishangge.persagysdk.uitls.EmptyViewUtils.OnNetWorkRetryListener;
import com.boruishangge.persagysdk.uitls.EmptyViewUtils.ShowExceptionUIHelper;
import com.boruishangge.persagysdk.uitls.StatusBarUtil;
import com.boruishangge.persagysdk.uitls.StringUtils;
import com.boruishangge.persagysdk.uitls.UIUtils;
import com.boruishangge.persagysdk.view.SVProgressDialog;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import com.example.yanfapersagy.sgyworkorder.Interface.IBaseView;

/**
 * Created by wuaomall@gmail.com on 2019/5/5.
 */
  public  abstract   class BaseActivity   extends RxAppCompatActivity   {

    /**
     * 空界面帮助工具
     */
    public ShowExceptionUIHelper mShowExceptionUIHelper;
    public static EventBus eventBus;
    public static Context context;
    private SVProgressDialog svProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        eventBus = EventBus.getDefault();
        eventBus.register(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        context = this;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    protected void showShortToast(String text) {
        if (!StringUtils.isEmpty(text)) {
            Toast.makeText(AppApplicationContext.context, text, Toast.LENGTH_SHORT).show();
        }
    }

    protected void showLongToast(String text) {
        if (!StringUtils.isEmpty(text)) {
            Toast.makeText(AppApplicationContext.context, text, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        eventBus.unregister(this);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 展示view需要改变的时候调用，一般界面只有一个
     * default findViewById(R.id.root)
     *
     * @param view
     */
    protected ShowExceptionUIHelper initShowExceptionView(View view, OnNetWorkRetryListener listener) {
        if (null != mShowExceptionUIHelper) {
            mShowExceptionUIHelper.resetContentView();
        }
        mShowExceptionUIHelper = new ShowExceptionUIHelper(this)
                .setCurrentView(view)
                .setOnNetworkErrorClickListener(() -> {
                    mShowExceptionUIHelper.refreshUI(ShowExceptionUIHelper.NORMAL_CONTENT);
                    if (null != listener) {
                        listener.retry();
                    }
                });
        return mShowExceptionUIHelper;
    }

    @Subscribe
    public void baseSubscribe(BaseEvent baseEvent) {

    }

     /**
     * 只有圆形进图条-背景灰色
     */
    public void show() {
        getHUD().show();
    }

    public boolean isShowing() {
        if (getHUD() != null) {
            return getHUD().isShowing();
        }
        return false;
    }

    /**
     * 进度条+文字-场景：网络请求等
     */
    public void showWithStatus(String info) {
        if (!isFinishing())
            getHUD().showWithStatus(info);
    }

    /**
     * 短暂提示功能
     *
     * @param info 提示信息
     */
    public void showInfoWithStatus(String info) {
        if (!isFinishing())
            getHUD().showInfoWithStatus(info);
    }

    /**
     * 短暂提示-比如：提交成功
     *
     * @param info 提示信息
     */
    public void showSuccessWithStatus(String info) {
        if (!isFinishing())
            getHUD().showSuccessWithStatus(info);
    }

    /**
     * 短暂提示-比如：提交失败
     *
     * @param info 提示信息
     */
    public void showErrorWithStatus(String info) {
        Log.e("tag", "showErrorWithStatus=" + info);
        if (!isFinishing())
            getHUD().showErrorWithStatus(info);
    }

    /**
     * 借助handler update progress-下载，上传等，看需求
     */
    public void showWithProgress() {

    }

    /**
     * 消失
     */
    public void dismiss() {
        if (svProgressDialog != null && svProgressDialog.isShowing()) {
            getHUD().dismiss();
        }
    }
    public SVProgressDialog getHUD() {
        if (svProgressDialog == null && !isFinishing()) {
            svProgressDialog = new SVProgressDialog(BaseActivity.this);
            svProgressDialog.setOnCancelListener(dialog -> this.onBackPressed());
        }
        return svProgressDialog;
    }

    /**
     * 白色 状态栏
     */
    public void whiteStatusBar() {
        if (UIUtils.FlymeSetStatusBarLightMode(getWindow(), true) || UIUtils.MIUISetStatusBarLightMode(getWindow(), true) || UIUtils.setAndroidOsSetStatusBarLightMode(getWindow(), true)) {
            StatusBarUtil.transparencyBar(this); //设置状态栏全透明
            StatusBarUtil.StatusBarLightMode(this); //设置白底黑字
            UIUtils.setWhiteBarColor(getWindow(), this, R.color.white_FFFFFF);

        }
    }


    /**
     * 蓝色 状态栏
     */
    public void blueStatusBar() {
        if (UIUtils.FlymeSetStatusBarLightMode(getWindow(), true) || UIUtils.MIUISetStatusBarLightMode(getWindow(), true) || UIUtils.setAndroidOsSetStatusBarLightMode(getWindow(), true)) {
            StatusBarUtil.transparencyBar(this); //设置状态栏全透明
            StatusBarUtil.StatusBarLightMode(this); //设置白底黑字
            UIUtils.setWhiteBarColor(getWindow(), this, R.color.FF0091FF);

        }
    }
    /**
     * 蓝色 状态栏
     */
    public void blueStatusBar(int color) {
        if (UIUtils.FlymeSetStatusBarLightMode(getWindow(), true) || UIUtils.MIUISetStatusBarLightMode(getWindow(), true) || UIUtils.setAndroidOsSetStatusBarLightMode(getWindow(), true)) {
            StatusBarUtil.transparencyBar(this); //设置状态栏全透明
            StatusBarUtil.StatusBarLightMode(this); //设置白底黑字
            UIUtils.setWhiteBarColor(getWindow(), this, color);

        }
    }


    /**
     * jinse 状态栏
     */
    public void jinseStatusBar() {
        if (UIUtils.FlymeSetStatusBarLightMode(getWindow(), true) || UIUtils.MIUISetStatusBarLightMode(getWindow(), true) || UIUtils.setAndroidOsSetStatusBarLightMode(getWindow(), true)) {
            StatusBarUtil.transparencyBar(this); //设置状态栏全透明
            StatusBarUtil.StatusBarLightMode(this); //设置白底黑字
            UIUtils.setWhiteBarColor(getWindow(), this, R.color.orange_FF9628);

        }
    }

    /**
     * heise 状态栏
     */
    public void heiseStatusBar() {
        if (UIUtils.FlymeSetStatusBarLightMode(getWindow(), true) || UIUtils.MIUISetStatusBarLightMode(getWindow(), true) || UIUtils.setAndroidOsSetStatusBarLightMode(getWindow(), true)) {
            StatusBarUtil.transparencyBar(this); //设置状态栏全透明
            UIUtils.setWhiteBarColor(getWindow(), this, R.color.black_272727);
        }
    }
}
